$(function() {
	// 数据格式化
	$("#btnFormat").bind("click", btnFormat);
	// 生成数据
	$("#btnGenerate").bind("click", btnGenerate);
	// 数据压缩
	$("#btnCompress").bind("click", btnCompress);
	// 清空数据
	$("#btnDelete").bind("click", btnDelete);	
    // 计数
    $("#btnCount").bind("click", btnCount);
    // 复制
    $("#btnCopy").bind("click", btnCopy);
    // 转义
    $("#btnReplace").bind("click", btnReplace);
    // 跳转
    $("#btnSkip").bind("click", btnSkip);
    // 显示或隐藏
    $("#developer").bind("click", showOrHide);
    // 隐藏不常用按钮
    btnHide();
});


/** 格式化数据 */
function btnFormat() {
	var jsonVal = $('#json_input').val();		
	try {
		$('#json_input').val(JSON.stringify(JSON.parse(jsonVal), null, "    "));
	} catch (parseException) {
		jsonVal = formatToJson(jsonVal);
        $('#json_input').val(jsonVal);
	}
}

/** 生成json数据 */
function btnGenerate() {
    $("#json_input").val('{"id":"1","sex":"男","age":"26","name":"孙小一","class":"三年一班","address":"上海","phone":"13816283964","like":"EAT"}');
}

/** 压缩数据 */
function btnCompress() {
	var jsonVal = $('#json_input').val();	
	$('#json_input').val(jsonVal.replace(/\s/g,""));
}

/** 删除数据 */
function btnDelete() {
	$("#json_input").val('');
}

/** 计数 */
function btnCount() {
    var jsonVal = $('#json_input').val().replace(/\s/g,"");
    $('#count').text(jsonVal.length);
}

/** 复制 */
function btnCopy() {
    document.getElementById("json_input").select();
    document.execCommand('copy', false, null);
}

/** 转义 */
function btnReplace() {
    var jsonVal = $('#json_input').val();
    // var reg = new RegExp("/\","g");
    $("#json_input").val(jsonVal.replace(/\\/g,""));
}

/** 跳转 */
function btnSkip() {
    var jsonVal = $('#json_input').val().replace(/\s/g,"");
    var skipWin = window.open('_blank');
    skipWin.location = "jsoncn.html?jsonVal="+jsonVal;
}

/** 显示或隐藏 */
function showOrHide() {
    var countNum = $("#countNum").val();
    if(countNum == '1'){
        btnShow();
    }else{
        btnHide();
    }
}

/** 显示 */
function btnShow() {
    $("#btnGenerate").show();
    $("#btnCompress").show();
    $("#btnCount").show();
    $("#btnReplace").show();
    $("#btnHide").show();

    $("#btnShow").hide();
    $("#countNum").val('0');
}

/** 隐藏 */
function btnHide() {
    $("#btnGenerate").hide();
    $("#btnCompress").hide();
    $("#btnCount").hide();
    $("#btnReplace").hide();
    $("#btnHide").hide();

    $("#btnShow").show();
    $("#countNum").val('1');
}

function repeat(s, count) {
	return new Array(count + 1).join(s);
}

function formatToJson(json) {
    var i = 0, il = 0, tab = "    ",newJson = "", indentLevel = 0, inString = false, currentChar = null;
    for (i = 0, il = json.length; i < il; i += 1) { 
        currentChar = json.charAt(i);

        switch (currentChar) {
        case '{': 
        case '[': 
            if (!inString) { 
                newJson += currentChar + "\n" + repeat(tab, indentLevel + 1);
                indentLevel += 1; 
            } else { 
                newJson += currentChar; 
            }
            break; 
        case '}': 
        case ']': 
            if (!inString) { 
                indentLevel -= 1; 
                newJson += "\n" + repeat(tab, indentLevel) + currentChar; 
            } else { 
                newJson += currentChar; 
            } 
            break; 
        case ',': 
            if (!inString) { 
                newJson += ",\n" + repeat(tab, indentLevel); 
            } else { 
                newJson += currentChar; 
            } 
            break; 
        case '，': 
            if (!inString) { 
                newJson += "，\n" + repeat(tab, indentLevel); 
            } else { 
                newJson += currentChar; 
            } 
            break;
        case ':': 
            if (!inString) { 
                newJson += ": "; 
            } else { 
                newJson += currentChar; 
            }
            break; 
        case ' ':
        case "\n":
        case "\t":
            if (inString) {
                newJson += currentChar;
            }
            break;
        case '"': 
            if (i > 0 && json.charAt(i - 1) !== '\\') {
                inString = !inString; 
            }
            newJson += currentChar; 
            break;
        case '\'': 
            if (i > 0 && json.charAt(i - 1) !== '\\') {
                inString = !inString; 
            }
            newJson += currentChar; 
            break;
        default: 
            newJson += currentChar; 
            break;                    
        } 
    } 
    return newJson; 
}